package com.wjd.wshow.service.impl;

import com.wjd.wshow.entity.Config;
import com.wjd.wshow.repository.ConfigRepository;
import com.wjd.wshow.service.ConfigService;
import org.springframework.stereotype.Service;

@Service("ConfigService")
public class ConfigServiceImpl extends BaseServiceImpl<Config, String> implements ConfigService {

    private final ConfigRepository configRepository;

    public ConfigServiceImpl(ConfigRepository configRepository) {
        this.configRepository = configRepository;
    }

    @Override
    public Config getConfig(String name) {
        return configRepository.getByName(name);
    }

    @Override
    public String saveConfig(Config config) {
        Config cfg = getConfig(config.getName());
        if (cfg != null) {
            config.setId(cfg.getId());
        }
        Config c = configRepository.save(config);
        return c.getId();
    }

    @Override
    public boolean deleteConfig(String name) {
        Config config = getConfig(name);
        if (config == null) {
            return false;
        }
        configRepository.delete(config);
        return true;
    }

    @Override
    public boolean updateConfig(Config config) {
        Config cfg = getConfig(config.getName());
        if (cfg == null) {
            return false;
        }
        config.setId(cfg.getId());
        configRepository.save(config);
        return true;
    }

    @Override
    protected ConfigRepository getRepository() {
        return configRepository;
    }
}
